﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MultiEnvironment.WebApplication1.Models;

namespace MultiEnvironment.WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public IConfiguration Config()
        {
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            var builder = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{environment}.json", true, reloadOnChange: true);

            var config = builder.Build();
            return config;
        }

        public IActionResult Index()
        {

            // An 2019-12-25 [ 显示当前环境变量 ]
            ViewBag.Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            // An 2019-12-25 [ 读取当前环境变量下配置文件中的节点 ]

            ViewBag.DefaultConnection = Config()["ConnectionString:Default"];


            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
